package com.hzzc.core.controller.back;

import com.hzzc.BizException;
import com.hzzc.Result;
import com.hzzc.core.model.SSOUser;
import com.hzzc.core.service.UserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/back")
public class BackUserController {

    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public Result<List<SSOUser>> users() {
        return Result.success(userService.userList(), "");
    }

    @PostMapping("/user")
    public Result<List<SSOUser>> user(@RequestBody UserBean userBean) throws BizException {
        if (userBean.getId() == null) {
            userService.addUser(userBean.name, userBean.head, userBean.account, userBean.pwd,userBean.black);
        } else {
            userService.updateUser(userBean.id, userBean.name, userBean.head, userBean.account, userBean.pwd,userBean.black);
        }
        return Result.success(userService.userList(), "");
    }

    @DeleteMapping("/delUser")
    public Result<List<SSOUser>> delUser(@RequestParam("userId") Long userId) {
        userService.delUser(userId);
        return Result.success(userService.userList(), "");
    }


    @Data
    static class UserBean {

        private Long id;

        private String name;

        private String head;

        private String account;

        private String pwd;

        private boolean black;

    }

}
